<?php
    namespace yaliSDK\db;

    use \think\Db;

    /**
     * Created by PhpStorm.
     * User: fang
     * Date: 2018/1/9
     * Time: 上午9:32
     */
    class TPDB implements IDB {
        private $connect; //数据库连接,默认连接第一个

        public function getHandler() {
            return;
        }
        public function __construct($config = []) {
            $this->connect = !empty($config) ? $config : config('database.db_config1');
        }

        /**
         * @param array $table 数据表
         * @param array $data 新增的数组
         * @param bool 插入成功,直接返回主键
         * @return mixed
         * 新增数据
         */
        public function insert(DBQuery $query): IDBResult {
            $table = $query->getTable();
            $data = $query->getData();
            try {
                $return_data = Db::connect($this->connect)->name($table)->insert($data, false, true);
                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult('');
            }
        }

        /**
         * @param array $table 数据表
         * @param array $where
         * @param array $data
         * @return mixed
         * 更新数据
         */
        public function update(DBQuery $query): IDBResult {
            $table = $query->getTable();
            $where = $query->getWhere();
            $data = $query->getData();
            try {
                $return_data = Db::connect($this->connect)->name($table)->where($where)->update($data);
                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult('');
            }
        }

        /**
         * @param array $table 数据表
         * @param array $where
         * @return mixed
         * 删除数据
         */
        public function delete(DBQuery $query): IDBResult {
            $table = $query->getTable();
            $where = $query->getWhere();
//            try {
                $return_data = Db::connect($this->connect)->name($table)->where($where)->delete();
//            echo Db::connect($this->connect)->getLastSql();die;
                return new TPDBResult($return_data);
//            } catch (\Exception $e) {
//                return new TPDBResult();
//            }
        }

        /**
         * @param string $table 表名
         * @param array $where 条件
         * @param bool $field 字段
         * @param null $join 连接表
         * @return bool|TPDBResult
         *
         */
        public function find(DBQuery $query): IDBResult {
            $table = $query->getTable();
            $where = $query->getWhere();
            $field = $query->getField();
            try {
                $query = Db::connect($this->connect)->name($table)->where($where)->field($field);
                if (!empty($join)) $query->join($join);
//            $query->find();
                $return_data = $query->find();
                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult(null);
            }
        }

        /**
         * @param DBQuery $query
         * @return IDBResult
         * 列表查询
         */
        public function list(DBQuery $query): IDBResult {

            // TODO: Implement list() method.
            try {
                $db_query = Db::connect($this->connect)->name($query->getTable());

                if (!empty($query->getAlias())) $db_query = $db_query->alias($query->getAlias());
                if (!empty($query->getWhere())) $db_query = $db_query->where($query->getWhere());
                if (!empty($query->getField())) $db_query = $db_query->field($query->getField());
                if (!empty($query->getJoin())) $db_query = $db_query->join($query->getJoin(),$query->getCondition());
                if (!empty($query->getOrder())) $db_query = $db_query->order($query->getOrder());
                if (!empty($query->getGroup())) $db_query = $db_query->group($query->getGroup());
                if (!empty($query->getLimit())) $db_query = $db_query->limit($query->getLimit());
                if (!empty($query->getPage())) $db_query = $db_query->page($query->getPage());
                $return_data = $db_query->select();
                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult([]);
            }
        }

        /**
         * @param DBQuery $query
         * @return IDBResult
         * 总数查询
         */
        public function count(DBQuery $query): IDBResult {
            // TODO: Implement count() method.
            try {
                $db_query = Db::connect($this->connect)->table($query->getTable());
                !empty($query->getAlias()) && $db_query = $db_query->alias($query->getAlias());
                !empty($query->getWhere()) && $db_query = $db_query->where($query->getWhere());
                !empty($query->getJoin()) && $db_query = $db_query->join($query->getJoin());
                !empty($query->getGroup()) && $db_query = $db_query->group($query->getGroup());
                $return_data = $db_query->count();
                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult(0);
            }
        }
        public function paginateList(DBQuery $query): IDBResult {
            // TODO: Implement list() method.
            try {
                $db_query = Db::connect($this->connect)->name($query->getTable());
                if (!empty($query->getAlias())) $db_query = $db_query->alias($query->getAlias());
                if (!empty($query->getWhere())) $db_query = $db_query->where($query->getWhere());
                if (!empty($query->getField())) $db_query = $db_query->field($query->getField());
                if (!empty($query->getJoin())) $db_query = $db_query->join($query->getJoin());
                if (!empty($query->getOrder())) $db_query = $db_query->order($query->getOrder());
                if (!empty($query->getGroup())) $db_query = $db_query->group($query->getGroup());

                $return_data = $db_query = $db_query->paginate($query->getLimit(),$query->getIsPaginate());

                return new TPDBResult($return_data);
            } catch (\Exception $e) {
                return new TPDBResult([]);
            }
        }
    }